{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "# Submit Training Job Demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import time\n",
    "import os\n",
    "import requests\n",
    "\n",
    "import fml_manager\n",
    "from fml_manager import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## For more details about the FMLManager, please refer to this [document](https://kubefate.readthedocs.io/README.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "The DSL and Config file can be presented in JOSN format. \n",
    "Submitting a job with JSON defined in line, can use ```submit_job(self,dsl, config)```. \n",
    "Note: the parameters are dict, the JSON string have to transform to dict with ```json.loads```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "# dsl\n",
    "secure_add_example = Component(name='secure_add_example_0',\n",
    "                               module='SecureAddExample')\n",
    "\n",
    "pipeline = Pipeline(\n",
    "    secure_add_example\n",
    ")\n",
    "\n",
    "# Configuration\n",
    "initiator = Initiator(role='guest',\n",
    "                      party_id=9999)\n",
    "\n",
    "job_parameters = JobParameters(work_mode=1)\n",
    "\n",
    "role = RoleBuilder()\\\n",
    "    .add_guest(9999)\\\n",
    "    .add_host(9999).build()\n",
    "\n",
    "secure_add_example_guest_config = {\n",
    "    \"seed\": [\n",
    "        123\n",
    "    ]\n",
    "}\n",
    "\n",
    "secure_add_example_host_config = {\n",
    "    \"seed\": [\n",
    "        321\n",
    "    ]\n",
    "}\n",
    "role_parameters = RoleParametersBuilder()\\\n",
    "    .add_host_module_config(module='secure_add_example_0', config=secure_add_example_host_config)\\\n",
    "    .add_guest_module_config(module='secure_add_example_0', config=secure_add_example_guest_config).build()\n",
    "\n",
    "secure_add_example = {\n",
    "    \"partition\": 10,\n",
    "    \"data_num\": 1000\n",
    "}\n",
    "\n",
    "algorithm_parameters = AlgorithmParametersBuilder()\\\n",
    "    .add_module_config(module='secure_add_example_0', config=secure_add_example).build()\n",
    "\n",
    "config = Config(\n",
    "    initiator,\n",
    "    job_parameters,\n",
    "    role,\n",
    "    role_parameters,\n",
    "    algorithm_parameters\n",
    ")\n",
    "\n",
    "manager = fml_manager.FMLManager()\n",
    "response = manager.submit_job(pipeline.to_dict(), config.to_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "manager.prettify(response, verbose=True)\n",
    "stdout = json.loads(response.content)\n",
    "job_id = stdout['jobId']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "query_condition = QueryCondition(job_id)\n",
    "\n",
    "job_status = manager.query_job(query_condition)\n",
    "manager.prettify(job_status, verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "manager.query_job_status(query_condition, max_tries=20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {}
   },
   "source": [
    "We can also fetch the logs of job submitted, and save it to working folder."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {}
   },
   "outputs": [],
   "source": [
    "response = manager.fetch_job_log(job_id)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
